<?php
/**
 * display.php
 *
 * - displays the pcap files. 
 * - The protocol layers can be matched with the left side against right side
 *
 *
 * LICENSE: 
 * This file is part of EasyShark.
 *
 *   EasyShark is free software: you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation, either version 3 of the License, or
 *   (at your option) any later version.
 *
 *   EasyShark is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *
 *   You should have received a copy of the GNU General Public License
 *   along with EasyShark.  If not, see <http://www.gnu.org/licenses/>.
 *
 * @author     Balazs Barcsik <balazs.barcsik@gmail.com>
 * @license    http://www.gnu.org/licenses/gpl.txt
 */

include 'config.php';

function f_processFile($windowId, $fileName, $filter = '', $decode = '', $line = 1) {

	global $root_folder;
	global $tshark;
	global $psml;
	global $pdml;
	# LOAD XML FILE
	$maxLine = $line + 50;
    if ($filter === '') {
	 $filterAtr = '"frame.number >= '.$line.' and frame.number < '.$maxLine.'"';
    } else {
    	$filterAtr = '"'.$filter.' and frame.number >= '.$line.' and frame.number < '.$maxLine.'"';
    }
    
	if(exec('echo EXEC') == 'EXEC'){
		//FIXME check both output of execution
		$command="$tshark -r " . $fileName." -T pdml -R ".$filterAtr." ".$decode." 2>&1 > $pdml";
		$return_string = exec($command , $output , $return_val );
		$command="$tshark -r " . $fileName." -T psml -R ".$filterAtr." ".$decode." 2>&1 > $psml";
		$return_string = exec($command , $output , $return_val );
		$outputString="";
		foreach ($output as $valami) {
			//echo $valami."<br>";
			$outputString=$valami."\n";
		}
		if($return_val != 0) {
			echo $outputString;
		} else {
		//FIXME call function for translation
		//echo $outputString;
		$XML = new DOMDocument();
		$XML->load( $psml, LIBXML_NOCDATA);
		//$XML->loadXML( $outputString, LIBXML_NOCDATA);
		# START XSLT
		$xslt = new XSLTProcessor();
		$XSL = new DOMDocument();
		$XSL->load( 'psml2html.xsl', LIBXML_NOCDATA);
		$xslt->importStylesheet( $XSL );
		#PRINT
		$xslt->setParameter('', 'window', $windowId);
		print $xslt->transformToXML( $XML );
		
		//echo $outputString;
		$XML = new DOMDocument();
		$XML->load( $pdml, LIBXML_NOCDATA);
		//$XML->loadXML( $outputString, LIBXML_NOCDATA);
		# START XSLT
		$xslt = new XSLTProcessor();
		$XSL = new DOMDocument();
		$XSL->load( 'pdml2html.xsl', LIBXML_NOCDATA);
		$xslt->importStylesheet( $XSL );
		#PRINT
		$xslt->setParameter('', 'window', $windowId);
		print $xslt->transformToXML( $XML );
		
		}
	} else {
	    echo "tshark cannot be executed<br>";
	}

}


$file1 = "";
$file2 = "";
$file1_withRoot = "";
$file2_withRoot = "";
$filter1 = "";
$decode1 = "";
$filter2 = "";
$decode2 = "";
$line1 = 1;
$line2 = 1;



if($_GET) {
	
	if(isset($_GET["file1"])) {
		$file1 = $_GET["file1"];
		$file1_withRoot = $root_folder.'/'.$_GET["file1"];
		
	}
	if(isset($_GET["file2"])) {
		$file2 = $_GET["file2"];
		$file2_withRoot = $root_folder.'/'.$_GET["file2"];
	} else {
		$file2 = $file1;
		$file2_withRoot = $file1_withRoot;
	}
	if(isset($_GET["filter1"])) {
	$filter1 = $_GET["filter1"];
	
	}
	if(isset($_GET["decode1"])) {
	$decode1 = $_GET["decode1"];
	}
	if(isset($_GET["filter2"])) {
	$filter2 = $_GET["filter2"];
	}
	if(isset($_GET["decode2"])) {
	$decode2 = $_GET["decode2"];
	}
	if(isset($_GET["line1"])) {
	$line1 = $_GET["line1"];
	}
	if(isset($_GET["line2"])) {
	$line2 = $_GET["line2"];
	}
}



print '<html>
<head>
<title>EasyShark</title>

</head>
<body>

';
print '<style>.octets {font-family: "Courier-New", Courier, monospace;} </style>';



print '<div style="width: 100%;overflow:auto;">';
print '<div style="float:left; width: 49%;">';

print '	<form action="index.php" method="get">';
print ' <input type="hidden" name="path" value="'. dirname($file1).'">';
print '	<input type="submit" value="Back">';
print '	</form>';

print	'<form action="display.php">';
print   '<input type="hidden" name="file1" value="'. $file1.'">';
print   '<input type="hidden" name="file2" value="'. $file2.'">';
print   '<input type="hidden" name="decode2" value="'. $decode2.'">';
print   '<input type="hidden" name="filter2" value="'.htmlentities(str_replace("\\","", $filter2), ENT_QUOTES).'">';
print   '<input type="hidden" name="line2" value="'. $line2.'">';

print	'Filter: <input type="text" value="'.htmlentities(str_replace("\\","", $filter1), ENT_QUOTES).'" name="filter1"><br/>';
print	'Decode as: <input type="text" value="'.$decode1.'" name="decode1"> i.e. -d tcp.port==3868,diameter<br/>';
print	'jump to frame: <input type="text" value="'.$line1.'" name="line1"> i.e. 15<br/>';

print	'<input type="submit" value="Filter">';
print	'</form>';
print	'File: '.$file1.' | <a href="download.php?file='.$file1.'">download</a>';
if($file1 != "") {
	f_processFile('left',$file1_withRoot,$filter1,$decode1, $line1);
}
print '<div id="frames1"></div>';
print '<div id="octets1" class="octets"></div>';
print '</div>';

print '<div style="float:right;width: 49%; ">';

print '	<form action="index.php" method="get">';
print ' <input type="hidden" name="file1" value="'. $file1.'">';
print '	<input type="submit" value="Select 2. file">';
print '	</form>';

print	'<form action="display.php">';
print   '<input type="hidden" name="file1" value="'. $file1.'">';
print   '<input type="hidden" name="file2" value="'. $file2.'">';
print   '<input type="hidden" name="decode1" value="'. $decode1.'">';
print   '<input type="hidden" name="filter1" value="'.htmlentities(str_replace("\\","", $filter1), ENT_QUOTES).'">';
print   '<input type="hidden" name="line1" value="'. $line1.'">';

print	'Filter: <input type="text" value="'.htmlentities(str_replace("\\","", $filter2), ENT_QUOTES).'" name="filter2"><br/>';
print	'Decode as: <input type="text" value="'.$decode2.'" name="decode2"> i.e. -d tcp.port==8090,http<br/>';
print	'jump to frame: <input type="text" value="'.$line2.'" name="line2"> i.e. 546<br/>';
print	'<input type="submit" value="Filter">';
print	'</form>';
print	'File: '.$file2.' | <a href="download.php?file='.$file2.'">download</a>';
if($file2 != "") {
	f_processFile('right',$file2_withRoot,$filter2,$decode2,$line2);
}
print '<div id="frames2"></div>';
print '<div id="octets2" class="octets"></div>';
print '</div>';
print '</div>';

print '<SCRIPT TYPE="text/javascript" SRC="diff_match_patch_uncompressed.js"></SCRIPT>
<SCRIPT>
var dmp = new diff_match_patch();
function launch() {
var text1 = document.getElementById("text1").innerHTML;
var text2 = document.getElementById("text2").innerHTML;
var d = dmp.diff_main(text1, text2);

dmp.diff_cleanupEfficiency(d);

var ds = dmp.diff_prettyHtml(d);
document.getElementById("outputdiv").innerHTML = ds + "<BR>";
}
</SCRIPT>';

print '
<div id="text1" style="display: none">text1</div>
<div id="text2" style="display: none">text2</div>

</div>
<FORM action="#" onsubmit="return false">
<INPUT TYPE="button" onClick="launch()" VALUE="Compute Diff"><br/>
</FORM>
<DIV ID="outputdiv"></DIV>';
print '
 

<div style="background-color:#efefe1;">
            <p><a href="http://code.google.com/p/easyshark/">EasyShark</a> server</p>
        </div>
</body></html>';
?>


